package com.koron.css2.serviceManage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserFieldBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.UserFieldConfigMapper;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 添加用户字段配置
 * 
 * @author 甄玉晴
 *
 */
public class UserFieldConfigAdd implements ServerInterface {

	private static Logger logger = LogManager.getLogger(UserFieldConfigAdd.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			//获取参数
			UserFieldBean bean = JsonUtils.objectToPojo(req.getData(), UserFieldBean.class);
			if(bean==null) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "缺少参数", void.class);
			}
			//获取mapper
			UserFieldConfigMapper mapper = factory.getMapper(UserFieldConfigMapper.class);
			//根据编号查询用户字段是否已存在
			UserFieldBean code=mapper.findUserFieldConfig(bean);
			if(code!=null) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "已存在相同标识得字段", void.class);
			}
			//添加
			userInfo.setCurWaterCode(bean.getGroupCode());
			bean.setCreateInfo(userInfo);
			mapper.insertUserFieldConfig(bean);
			return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
		} catch (Exception e) {
			logger.error("用户字段配置增加失败", e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "增加失败", void.class);
		}
	}

}
